package indi.kider.server.woniubus.api;

import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.woniubus.LeaseBusAndRent;
import indi.kider.common.model.woniubus.LeaseBusQuery;
import indi.kider.server.woniubus.service.LeaseBusService;
import indi.kider.server.woniubus.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 退租和换房业务申请处理
 */
@RestController
@RequestMapping(value = "leaseBus")
public class LeaseBusController {

    @Autowired
    private LeaseBusService leaseBusService;
    @Autowired
    private AuthcService authcService;


    /**
     * 审核暂时由宿管处理
     * @api {POST} /queryLeaseBusListForSh
     * @apiGroup wnbus
     * @apiDescription 根据字段查询获取待审核列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/queryLeaseBusListForSh
     */
    @PostMapping("/queryLeaseBusListForSh")
    public CommonResponse<PageResponse<List<Map>>> queryLeaseBusListForSh(@RequestBody PageRequest<LeaseBusQuery> request) {
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(leaseBusService.queryLeaseBusListForSh(request)));
    }

    /**
     * @api {PUT} /sh
     * @apiGroup leaseBus
     * @apiDescription 审核
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/sh
     * {"id":null,review:"问题描述"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/sh")
    public CommonResponse sh(@RequestBody LeaseBusQuery leaseBus) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            leaseBus.setReviewTime(new Date());
            leaseBus.setReviewOperator(employee.getName());
            return leaseBusService.sh(leaseBus);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /shErr
     * @apiGroup leaseBus
     * @apiDescription 审核驳回
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/shErr
     * {"id":null,review:"问题描述"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/shErr")
    public CommonResponse shErr(@RequestBody LeaseBusQuery leaseBus) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            leaseBus.setReviewTime(new Date());
            leaseBus.setReviewOperator(employee.getName());
            return leaseBusService.shErr(leaseBus);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * 宿管审核
     * @api {POST} /queryLeaseBusListForGjSh
     * @apiGroup leaseBus
     * @apiDescription 根据字段查询获取待审核列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/queryLeaseBusListForGjSh
     */
    @PostMapping("/queryLeaseBusListForGjSh")
    public CommonResponse<PageResponse<List<Map>>> queryLeaseBusListForGjSh(@RequestBody PageRequest<LeaseBusQuery> request) {
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(leaseBusService.queryLeaseBusListForGjSh(request)));
    }


    /**
     * @api {PUT} /gjsh
     * @apiGroup leaseBus
     * @apiDescription 审核
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/gjsh
     * {"id":null,review:"问题描述"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/gjsh")
    public CommonResponse gjsh(@RequestBody LeaseBusAndRent leaseBusAndRent) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            leaseBusAndRent.getLeaseBus().setHousekeeperTime(new Date());
            leaseBusAndRent.getLeaseBus().setHousekeeperOperator(employee.getName());
            leaseBusAndRent.getRent().setOpOutId(employee.getId());
            return leaseBusService.gjsh(leaseBusAndRent);
        } catch (UnexpectedRollbackException ee){
            return CommonResponse.buildBadRequestResponse("请检查1所换房间是否满员|男女混住,2用户身份证|手机号是否被使用",null);
        }catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /gjshErr
     * @apiGroup leaseBus
     * @apiDescription 审核驳回
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/leaseBus/gjshErr
     * {"id":null,review:"问题描述"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/gjshErr")
    public CommonResponse gjshErr(@RequestBody LeaseBusQuery leaseBus) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            leaseBus.setHousekeeperTime(new Date());
            leaseBus.setHousekeeperOperator(employee.getName());
            return leaseBusService.gjshErr(leaseBus);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
